package vip.xiaonuo.supplier.modular.finance.settlement.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.supplier.modular.finance.settlement.entity.SupplierSettlement;
import vip.xiaonuo.supplier.modular.finance.settlement.param.*;
import vip.xiaonuo.supplier.modular.finance.settlement.service.SupplierSettlementService;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import vip.xiaonuo.supplier.modular.warehouse.storesstream.param.SupplierStoresStreamExportParam;

import java.io.IOException;
import java.util.List;

/**
 * 结算单表控制器
 *
 * @author Mr.wu
 * @date  2025/04/09 17:28
 */
@Tag(name = "结算单表控制器")
@RestController
@Validated
public class SupplierSettlementController {

    @Resource
    private SupplierSettlementService supplierSettlementService;

    /**
     * 获取结算单表分页
     *
     * @author Mr.wu
     * @date  2025/04/09 17:28
     */
    @Operation(summary = "获取结算单表分页")
    @SaCheckPermission("/supplier/finance/settlement/page")
    @GetMapping("/supplier/finance/settlement/page")
    public CommonResult<Page<SupplierSettlement>> page(SupplierSettlementPageParam supplierSettlementPageParam) {
        return CommonResult.data(supplierSettlementService.page(supplierSettlementPageParam));
    }

    /**
     * 添加结算单表
     *
     * @author Mr.wu
     * @date  2025/04/09 17:28
     */
    @Operation(summary = "添加结算单表")
    @CommonLog("添加结算单表")
    @SaCheckPermission("/supplier/finance/settlement/add")
    @PostMapping("/supplier/finance/settlement/add")
    public CommonResult<String> add(@RequestBody @Valid SupplierSettlementAddParam supplierSettlementAddParam) {
        supplierSettlementService.add(supplierSettlementAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑结算单表
     *
     * @author Mr.wu
     * @date  2025/04/09 17:28
     */
    @Operation(summary = "编辑结算单表")
    @CommonLog("编辑结算单表")
    @SaCheckPermission("/supplier/finance/settlement/edit")
    @PostMapping("/supplier/finance/settlement/edit")
    public CommonResult<String> edit(@RequestBody @Valid SupplierSettlementEditParam supplierSettlementEditParam) {
        supplierSettlementService.edit(supplierSettlementEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除结算单表
     *
     * @author Mr.wu
     * @date  2025/04/09 17:28
     */
    @Operation(summary = "删除结算单表")
    @CommonLog("删除结算单表")
    @SaCheckPermission("/supplier/finance/settlement/delete")
    @PostMapping("/supplier/finance/settlement/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<SupplierSettlementIdParam> supplierSettlementIdParamList) {
        supplierSettlementService.delete(supplierSettlementIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取结算单表详情
     *
     * @author Mr.wu
     * @date  2025/04/09 17:28
     */
    @Operation(summary = "获取结算单表详情")
    @SaCheckPermission("/supplier/finance/settlement/detail")
    @GetMapping("/supplier/finance/settlement/detail")
    public CommonResult<SupplierSettlement> detail(@Valid SupplierSettlementIdParam supplierSettlementIdParam) {
        return CommonResult.data(supplierSettlementService.detail(supplierSettlementIdParam));
    }

    /**
     * 结算单据导出含汇总
     *
     * @author Mr.wu
     * @date 2025/04/18 20:00
     */
    @Operation(summary = "结算单据导出含汇总")
    @CommonLog("结算单据导出")
    @SaCheckPermission("/supplier/finance/settlement/export")
    @GetMapping(value = "/supplier/finance/settlement/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportStream(SupplierSettlementExportParam supplierSettlementExportParam, HttpServletResponse response) throws IOException {
        supplierSettlementService.exportSettlement(supplierSettlementExportParam, response);
    }
}
